package it.amicosmanettone.finance.stocky;

import java.util.Locale;

import it.amicosmanettone.finance.stocky.R;
import it.amicosmanettone.finance.stocky.archive.ArchiveManager;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;

public class SplashActivity extends Activity {

	private static int SPLASH_TIME_OUT = 2500;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_splash);
		
		final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		

		new Handler().postDelayed(new Runnable() {
			
			Configuration config = new Configuration();

			@Override
			public void run() {
				
				String START_APP_LANGUAGE = sharedPref.getString("start_app_language", "ENGLISH");
				if (START_APP_LANGUAGE.equals("ENGLISH")) {
					
					
					config.locale = Locale.ENGLISH;
					getResources().updateConfiguration(config, null);

				}
				if (START_APP_LANGUAGE.equals("ITALIAN")) {
					
					
					config.locale = Locale.ITALIAN;
					getResources().updateConfiguration(config, null);

				}

				String defaultValue = getText(R.string.prefUpdateStart).toString();
				Log.e("SPLASH", "Recupero la preference");
				String UPDATE = sharedPref.getString("pref_list_update", defaultValue);
				
				if(UPDATE.equals(defaultValue)){
					Log.e("SPLASH", "Aggiorno come da preference");
					ArchiveManager.retrieveAllPortfolios(getApplicationContext());
					
				}

				Intent i = new Intent(SplashActivity.this, MainActivity.class);
				i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
				startActivity(i);
			}
		}, SPLASH_TIME_OUT);

	}

}
